<?php


namespace app\admin\model;


use think\Db;
use think\db\Query;
use think\facade\Env;
use think\facade\Request;
use think\Model;

class Duty extends Model
{
    protected $autoWriteTimestamp = true;
    /**
     * @param $dutyData
     * @return bool
     * @throws \Exception
     */
    public static function Add($dutyData):bool
    {
        $duty = new Duty();
        $bool = $duty->delete();
        if (!$bool) {
            return false;
        }
        $list = [];
        foreach ($dutyData as $k => $v) {
            $data = [
                "id" => $v["DutyId"],
                "name" => $v["DutyName"],
            ];
            $list[$k] = $data;
        }
        $duty->saveAll($list);
        return true;
    }

    /**
     * @param $dutyId
     * @return string
     */
    public static function getDutyNameById($dutyId):string
    {
        $dutyInfo = Duty::where("id",$dutyId)->find();
        if (!$dutyInfo){
            return "";
        }
        return $dutyInfo["name"];
    }

    /**
     * @param $data
     * @throws \Exception
     * 覆盖添加
     */
    public static function coverDutyAdd($data)
    {
        Db::name('duty')->delete(true);
        $list = [];
        foreach ($data as $k => $v){
            $list[] = ["id"=>$v["DutyId"],"name"=>$v["DutyName"]] ;
        }
        $d = new Duty();
        $d->saveAll($list,false);
    }

    // 权限控制
    public static function Authorization($staffNo)
    {
        $be_man = Env::get("STAFF.SERVER_BE_DUTY_MAN");
        $af_man = Env::get("STAFF.SERVER_AF_DUTY_MAN");

        $arrMan = explode(",",$be_man.",".$af_man);
        if($staffNo == "ic_admin" || $staffNo == "PXS000500") return "";
        $dutyId = Staff::where("staff_no",$staffNo)->value("duty_id");
        if(!in_array($dutyId,$arrMan)){
            return "不是管理人员";
        }
        return "";
    }

    /**
     * @param $type
     * @return array|false|string[]
     * 获取售前售后职位数据
     */
    public static function getServerData($type)
    {
        if($type == "be"){
            $be_man = Env::get("STAFF.SERVER_BE_DUTY_MAN");
            $be = Env::get("STAFF.SERVER_BE_DUTY");
            $arrMan = explode(",",$be_man);
            $arr = explode(",",$be);
            return array_merge($arrMan,$arr);
        }

        if($type == "af"){
            $af_man = Env::get("STAFF.SERVER_AF_DUTY_MAN");
            $af = Env::get("STAFF.SERVER_AF_DUTY");
            $arrMan = explode(",",$af_man);
            $arr = explode(",",$af);
            return array_merge($arrMan,$arr);
        }
        return [];
    }

    // v1.0.0获取数据权限
    public static function getDataAuth($sta = ""):array
    {
        if($sta){
            $my = $sta;
        }else{
            $my = Request::param("_user");
        }
        if($my["server_type"] == "be"){
            $be_man = Env::get("STAFF.SERVER_BE_DUTY_MAN");
            $be = Env::get("STAFF.SERVER_BE_DUTY");
            $arrMan = explode(",",$be_man);
            $arr = explode(",",$be);
            if(in_array($my["duty_id"],$arrMan)){
                $RR = array_merge($arrMan,$arr);
                return [1,$RR]; // 售前 负责人
            }
            if(in_array($my["duty_id"],$arr)){
                return [2,$arr]; // 售前 客服
            }

        }else if($my["server_type"] == "af"){
            $af_man = Env::get("STAFF.SERVER_AF_DUTY_MAN");
            $af = Env::get("STAFF.SERVER_AF_DUTY");
            $arrMan = explode(",",$af_man);
            $arr = explode(",",$af);

            if(in_array($my["duty_id"],$arrMan)){
                $RR = array_merge($arrMan,$arr);
                return [3,$RR]; // 售后 负责人
            }
            if(in_array($my["duty_id"],$arr)){
                return [4,$arr]; // 售后 客服
            }
        } else if($my["server_type"] == "super"){
            return [5,[]];  // 全部
        }
        return [0,[]];
    }

    /**
     * v1.0.0根据数据权限id,获取 查询范围
     * @param Query $query
     * @param string $staffNo 负责人
     * @return Query
     */
    public static function getQueryByDataPermIdV1_0_0(Query $query, $staffNo = "staff_no"):Query
    {

        $my = Request::param("_user");
        // 获取职位权限
        list($data_perm_id,$dutyArr) = Duty::getDataAuth();
        // 获取人员
//        $staffNoArr = Staff::getStaffNoByDuty($dutyArr);
        $staffNoArr = Staff::getDataByFiled($dutyArr,$staffNo);

        switch ($data_perm_id) {
            case 0:
            case 2:
            case 4:
                //自己
                $query->where($staffNo, $my->staff_no);
                break;
            case 3:
            case 1:
                $query->where($staffNo, "in", $staffNoArr);
                break;
            case 5:
                //所有
            default:
                //无
                break;
        }
        return $query;
    }

    // v1.0.1获取数据权限 (售前与售后)
    public static function getDataDepartment():array
    {
        $my = Request::param("_user");

        $be_man = Env::get("STAFF.SERVER_BE_DUTY_MAN");
        $be = Env::get("STAFF.SERVER_BE_DUTY");
        $arrMan = explode(",",$be_man);
        $arr = explode(",",$be);
        if(in_array($my["duty_id"],array_merge($arrMan,$arr))){
            return array_merge($arrMan,$arr); // 售前 负责人
        }

        $af_man = Env::get("STAFF.SERVER_AF_DUTY_MAN");
        $af = Env::get("STAFF.SERVER_AF_DUTY");
        $arrMan = explode(",",$af_man);
        $arr = explode(",",$af);
        if(in_array($my["duty_id"],array_merge($arrMan,$arr))){
            return array_merge($arrMan,$arr); // 售后 负责人
        }

        if($my["staff_no"] == "ic_admin" || $my["staff_no"] == "PXS000500"){
            return array_merge($arrMan,$arr); // 售后 负责人
        }
        return [];
    }

    /**
     * v1.0.1根据数据权限id,获取 查询范围
     * @param Query $query
     * @param string $field 字段
     * @return Query
     */
    public static function getQueryByDataPermId(Query $query, $field = "staff_no"):Query
    {
        $my = Request::param("_user");
        $data_perm_id = $my["role_data"];
        // 获取职位
        $dutyArr = Duty::getDataDepartment();

        switch ($data_perm_id) {
            case 0:
            case 1:
                 //自己
                $query->where($field, $my->staff_no);
                break;
            case 2:
                // 获取人员
                $fieldArr = Staff::getStaffNoByStaffNo($my->staff_no);
                // 部门
                $query->where($field, "in", $fieldArr);
                break;
            case 3:
                // 获取人员
                $fieldArr = Staff::getStaffNoByDuty($dutyArr);
                // 部门
                $query->where($field, "in", $fieldArr);
                break;
            case 4:
                // 全部
                break;
            default:
                //无
                break;
        }
        return $query;
    }

    // 权限url控制
    public static function UrlAuthority($sta):string
    {
        list($sta) = Duty::getDataAuth($sta);
        if($sta == 1 ||$sta == 3||$sta == 5)return "manage";
        if($sta == 2 ||$sta == 4)return "customer";
        return "";
    }



}